package com.bdzl.ecs.controller;

import com.alibaba.fastjson.JSON;
import com.bdzl.ecs.service.ContractTmplService;
import com.bdzl.ecs.util.contractUtils.ContractUtils;
import com.bdzl.pub.controller.BaseController;
import com.bdzl.pub.entity.loan.ContractTmplDO;
import com.bdzl.pub.util.file.BASE64DecodedMultipartFile;
import com.bdzl.pub.util.file.OssUploadFileUtil;
import com.bdzl.pub.util.param.PageUtils;
import com.bdzl.pub.util.param.Query;
import com.bdzl.pub.util.param.R;
import com.bdzl.pub.vo.loan.ContractTmplVO;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 电子合同模板表
 *
 * @author yangyicai
 * @email yangyicai@foxmail.com
 * @date 2021-03-23 14:00:08
 */

@Controller
@RequestMapping("/bdzl/contractTmpl")
public class ContractTmplController extends BaseController {
	@Autowired
	private ContractTmplService contractTmplService;

	@GetMapping()
	String ContractTmpl(){
	    return "bdzl/contractTmpl/contractTmpl";
	}

	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<ContractTmplDO> contractTmplList = contractTmplService.list(query);
		int total = contractTmplService.count(query);
		PageUtils pageUtils = new PageUtils(contractTmplList, total);
		return pageUtils;
	}

	@ResponseBody
	@GetMapping("/listAndUser")
	public PageUtils listAndUser(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<ContractTmplVO> contractTmplList = contractTmplService.listAndUser(query);
		int total = contractTmplService.listAndUserCount(query);
		PageUtils pageUtils = new PageUtils(contractTmplList, total);
		return pageUtils;
	}

	@GetMapping("/add")
	String add(){
	    return "bdzl/contractTmpl/add";
	}

	@GetMapping("/edit/{tmplId}")
	String edit(@PathVariable("tmplId") Integer tmplId,Model model){
		ContractTmplDO contractTmpl = contractTmplService.get(tmplId);
		model.addAttribute("contractTmpl", contractTmpl);
	    return "bdzl/contractTmpl/edit";
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( ContractTmplDO contractTmpl){
		if(contractTmplService.save(contractTmpl)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( ContractTmplDO contractTmpl){
		contractTmplService.update(contractTmpl);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/remove/{tmplId}")
	@ResponseBody
	public R remove(@PathVariable("tmplId") Integer tmplId){
		ContractTmplDO contractTmpl = contractTmplService.get(tmplId);
		if(contractTmplService.remove(tmplId)>0){
			OssUploadFileUtil.remove(contractTmpl.getTmplUrl());
		return R.ok();
		}
		return R.error();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Integer[] tmplIds){
		contractTmplService.batchRemove(tmplIds);
		return R.ok();
	}

	/**
	 * zhaochunyu
	 * 上传合同模板，参数字段 file和doc_url选一个
	 * @Date 2021年4月26日16:01:20
	 * @param contractTmpl PDF模板 字段类型 文件流
	 * @return
	 */
	@PostMapping("/uploadTemplate")
	@ResponseBody
	public R uploadTemplate(@RequestBody ContractTmplDO contractTmpl){
		//模板编号 只允许长度<=32的英文或数字字符
		String template_id = System.currentTimeMillis()+"";
		template_id = template_id.substring(template_id.length()-10,template_id.length());
		//MultipartFile file1 = BASE64DecodedMultipartFile.base64ToMultipart(file);
		String docUrl = OssUploadFileUtil.RandomFileName.uploadToOSS(contractTmpl.getTmplUrl(), "loan/" +"contractUtils");
		String response= ContractUtils.uploadTemplate(template_id,null,docUrl);
		HashMap<String,String> responseMap = JSON.parseObject(response,HashMap.class);
		String code = responseMap.get("code");
		if("1".equals(code)){//1 代表成功
			contractTmpl.setTmplNo(Long.parseLong(template_id));
			contractTmpl.setTmplUrl(docUrl);
			contractTmpl.setTmplCreuser(super.getUserId());
			contractTmplService.save(contractTmpl);
		}
		return R.ok(responseMap);
	}
}
